﻿using System;

using System.Data;

using System.Web.UI.WebControls;

using Model;
using Service;

public partial class GroupeSoftwares : System.Web.UI.Page
{
    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    EquipmentSystem _system = new EquipmentSystem();
    private DirectService _DirectService = new DirectService();

    protected void Page_Load(object sender, EventArgs e)
    {
       // lblTotalGroupeSoftware.Text = "Total group software:" + _ReferenceDataService.GetGroupeSoftware().Count;

        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;

        new Utility().SetRowToDisplay(grdGroupeSoftware, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {
            // to avoid loading for the first time

            if (Session["pageIndex"] != null)
            {
                grdGroupeSoftware.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }



            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
            txtHiddenPageIndex.Text = "0";

        }
        else
        {
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            lblTotalGroupeSoftware.Text = "Total group software : " + new DirectService().GetGroupeSoftwareDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectService().GetGroupeSoftwareDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdGroupeSoftware1.Visible = true;
            }
            else
            {
                grdGroupeSoftware.Visible = true;
                grdGroupeSoftware1.Visible = false;
            }
        }

    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtGroupeSoftwareId.Text))
        {
            if (int.TryParse(txtGroupeSoftwareId.Text, out i))
            {
                and = " and ";
                str = " where GroupeSoftware.GroupeSoftwareId=" + Int32.Parse(txtGroupeSoftwareId.Text);
                if (txtGroupeSoftwareLabel.Text != string.Empty)
                {
                    str += " " + "and" + " " + "GroupeSoftware.GroupeSoftware=" + "'" + txtGroupeSoftwareLabel.Text +
                           "'";
                    and = " and ";
                }
            }else
            {
                grdGroupeSoftware.Visible = false;
                grdGroupeSoftware1.Visible = true;
                lblTotalGroupeSoftware.Text = "Total group software : 0";
                return null;
            }
        }
        else
        {
            if (txtGroupeSoftwareLabel.Text != string.Empty)
            {
                str += " " + "where" + " " + "GroupeSoftware.GroupeSoftware like" + "'%" + txtGroupeSoftwareLabel.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by GroupeSoftware.GroupeSoftware";
    }

    protected void bntFilter_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlSearch();

    }
    private void ClearControlSearch()
    {
        txtGroupeSoftwareId.Text = "";
        txtGroupeSoftwareLabel.Text = ""; 
    }
    protected void grdEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grdGroupeSoftware.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdGroupeSoftware.PageIndex.ToString();
        Session["pageIndex"] = grdGroupeSoftware.PageIndex;

    }
    protected void grdEquipment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void grdSystem_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Delete"))
        {
            int groupeSoftwareId = Convert.ToInt32(grdGroupeSoftware.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            _StockService.DeleteGroupeSoftware(groupeSoftwareId);
            Response.Redirect("GroupeSoftwares.aspx");
        }
        new Utility().SetSelectedColorToGridView(e, grdGroupeSoftware);
    }
    protected void grdSystem_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grdSystem_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdSystem_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void grdSystem_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void grdEquipment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdSystem_Sorted(object sender, EventArgs e)
    {

    }
    protected void grdEquipment_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //block to add message
            string stringComplete = "";
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
                deleteButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.ConfirmDeleteMessage+ "');");
            }
        }

    }
    protected void grdSystem1_SelectedIndexChanged(object sender, EventArgs e)
    {


    }

    protected void dsGroupeSoftware_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalGroupeSoftware.Text = "Total groupsoftware:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
}
